import "JSsend_to_usart"
import { CommunicationPack, ParameterPack } from "JSsend_to_usart";
import { md5 } from "MD5";


var action_list = []
var runningAction;
var actionComplete;
var acted_name = "";
export const Action={
	request(param, ser){
		if( action_list.length != 0){
			var action_name = action_list.pop(0);
		}
		else{
			action_name = 'default';
		}
			
		if( Debug_flag){concole.log('REQUEST!');}
		const para_pack=new ParameterPack(action_name);
		const commu_pack=new CommunicationPack(null,para_pack);
		const U8_commu_pack = commu_pack.toU8Array();
		ser.write(res)
	},
	start(param, ser) {
		runningAction = true;
		if (Debug_flag) {
			console.log('start len(list):', action_list.length);
		}
	},
	complete(param, ser) {
		actionComplete = True
		if (Debug_flag) {
			concole.log('complete ... True ');
		}
	},
	wait() {
		if (actionComplete == false) {
			var hang_on = true;
			setTimeout(function () { hang_on = false; }, 30);
			while (hang_on && (action_list.length != 0 || actionComplete == false)) {
				time.sleep(0.001);
			}
		}
		runningAction = false;
	},
	append(str_act_name){
		var uint8array_name = new TextEncoder("utf-8").encode(str_act_name);
	
		acted_name = 'leju_' + md5(uint8array_name)
	
		actionComplete = false;
		if (action_list.length() > 0) {
			concole.log("队列超过一个动作")
			action_list.push(acted_name)
		}
			
		else{
			action_list.push(acted_name)
		}
			
	
		if(Debug_flag){
			concole.log("执行动作名：",act_name)
		}
			
		this.wait()
	}
}

